package com.open.capacity.reimburse.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.afterturn.easypoi.entity.vo.TemplateExcelConstants;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.view.PoiBaseView;
import com.open.capacity.commons.Result;
import com.open.capacity.reimburse.dto.report.BusinesstripBcDto;
import com.open.capacity.user.model.SysCompany;
import com.open.capacity.user.service.SysCompanyService;
import com.open.capacity.utils.DateUtil;
import com.open.capacity.workflow.model.WFEngine.WFContent;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import com.open.capacity.reimburse.entity.BillProjectApply;
import com.open.capacity.reimburse.service.BillProjectApplyService;

import com.open.capacity.commons.PageResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * 立项申请表
 *
 * @author
 * @email
 * @date 2021-06-29 10:26:53
 */
@RestController
@RequestMapping("projectapplys")
@Api(tags = "立项申请表")
public class BillProjectApplyController {

    @Autowired
    private BillProjectApplyService billProjectApplyService;

    @Autowired
    private SysCompanyService sysCompanyService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    @PreAuthorize("hasAnyAuthority('bill_project_apply:billprojectapply:list')")
    public PageResult list(@RequestParam Map<String, Object> params) {
        PageResult pageResult = billProjectApplyService.pageAll(params);
        return pageResult;
    }

    /**
     * 查询单条记录
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAnyAuthority('bill_project_apply:billprojectapply:list')")
    public Result findById(String id) {
        return Result.succeed(billProjectApplyService.findById(id));
    }

    /**
     * 查询单条记录
     */
    @RequestMapping("/findByProcessId")
    @PreAuthorize("hasAnyAuthority('bill_project_apply:billprojectapply:list')")
    public Result findByProcessId(String processId) {
        return Result.succeed(billProjectApplyService.findById(processId));
    }


    /**
     * 保存
     */
    @RequestMapping("/saveOrUpdate")
    @PreAuthorize("hasAnyAuthority('bill_project_apply:billprojectapply:save')")
    public Result save(@RequestBody @Valid BillProjectApply billProjectApply, String keyValue) {
        if (StringUtils.isEmpty(keyValue)) {
            billProjectApplyService.save(billProjectApply);
        } else {
            billProjectApply.setId(keyValue);
            billProjectApplyService.update(billProjectApply);
        }
        return Result.succeed("操作成功");
    }


    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAnyAuthority('bill_project_apply:billprojectapply:delete')")
    public Result delete(@PathVariable String id) {
        billProjectApplyService.delete(id);
        return Result.succeed("删除成功");
    }


    /**
     * 送审
     */
    @PostMapping("/send")
    @PreAuthorize("hasAnyAuthority('bill_project_apply:billprojectapply:send')")
    public Result create(Boolean isNew, String processId, String schemeCode, String processName, Integer processLevel, String description, String auditors, String formData) {
        Result<WFContent> res = billProjectApplyService.send(isNew, processId, schemeCode, processName, processLevel, description, auditors, formData);
        return res;
    }

    /**
     * 取消送审
     */
    @PostMapping("/cancel")
    @PreAuthorize("hasAnyAuthority('bill_project_apply:billprojectapply:cancel')")
    public Result cancel(String processId) {
        int count = billProjectApplyService.cancel(processId);
        return Result.succeed(count);
    }

    /**
     * 差旅费备查表导出excel
     *
     * @return
     */
    @GetMapping("/exportExcel")
    public void exportExcel(@RequestParam Map<String, Object> params, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        TemplateExportParams paramsExcel = new TemplateExportParams("assets/project_target.xls");
        String processId = params.get("processId").toString();
        BillProjectApply bill = billProjectApplyService.findById(processId);

        SysCompany company = sysCompanyService.findById(bill.getCompanyId());

        map.put("code", bill.getCode());
        map.put("name", bill.getName());
        map.put("companyName", company.getFullName());
        map.put("totalMoney", bill.getTotalMoney());
        map.put("attribute", bill.getAttribute());
        map.put("chargePerson", bill.getChargePerson());
        map.put("category", bill.getCategory());
        map.put("summary", bill.getSummary());
        map.put("description", bill.getDescription());

        modelMap.put(TemplateExcelConstants.FILE_NAME, "预算项目和绩效目标申报表");
        modelMap.put(TemplateExcelConstants.PARAMS, paramsExcel);
        modelMap.put(TemplateExcelConstants.MAP_DATA, map);
        PoiBaseView.render(modelMap, request, response, TemplateExcelConstants.EASYPOI_TEMPLATE_EXCEL_VIEW);
    }

}
